package com.muye.dianli;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.entity.mime.content.StringBody;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.google.gson.Gson;
import com.muye.constant.C;
import com.muye.constant.U;
import com.muye.field.HomePageList;
import com.muye.field.HomePageListData;
import com.zvezda.algorithm.utils.MD5;
import com.zvezda.http.utils.HttpConst;
import com.zvezda.http.utils.KeyValue;
import com.zvezda.http.utils.RequestHttpListener;
import com.zvezda.http.utils.RequestHttpPostThread;

/**
 * Logo界面(入口Activity)
 * 
 * @author TangChao
 *
 */
public class LogoActivity extends Activity
{
	/**
	 * 计时器
	 */
	private int time = 0;
	
	/**
	 * 计时线程
	 */
	private Runnable runnable = new Runnable()
	{
		
		@Override
		public void run()
		{
			time++;
			handler.postDelayed(runnable, 1000);
			Log.i(C.TAG, "倒计时----->" + time);
			if (time > 1)
			{
				Intent intent = new Intent(LogoActivity.this, MainActivity.class);
				startActivity(intent);
				finish();
			}
		}
	};
	
	/**
	 * 请求数据的线程
	 */
	private RequestHttpPostThread requestHttpPostThread;
	
	/**
	 * 数据源
	 */
	private List<List<HomePageListData>> dataList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		getWindow().setFormat(PixelFormat.RGBA_8888);
		setContentView(R.layout.logo_layout);
		getHomePageList();
	}
	
	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		handler.removeCallbacks(runnable);
	}
	
	/**
	 * 请求首页列表
	 */
	private void getHomePageList()
	{
		ArrayList<KeyValue<String, Object>> paramsList = new ArrayList<KeyValue<String, Object>>();
		String str = C.APPID + C.CONSTANT;
		try
		{
			paramsList.add(new KeyValue<String, Object>("appId", new StringBody(C.APPID, Charset.forName("utf-8"))));
			paramsList.add(new KeyValue<String, Object>("sign", new StringBody(MD5.getMD5_32(str), Charset.forName("utf-8"))));
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		requestHttpPostThread = new RequestHttpPostThread(getHomePagerListener, U.HOME_PAGE_LIST, paramsList);
		requestHttpPostThread.start();
	}
	
	/**
	 * 请求首页列表返回监听
	 */
	private RequestHttpListener getHomePagerListener = new RequestHttpListener()
	{
		
		@Override
		public void requestHttp(Message msg)
		{
			Bundle bundle = msg.getData();
			int result = bundle.getInt(HttpConst.ACTION_RESULT, HttpConst.ACTION_FAIL);
			if (result == HttpConst.ACTION_SUCCESS)
			{
				byte[] dataArr = bundle.getByteArray(HttpConst.DATA_ARR);
				try
				{
					String str = new String(dataArr, "utf-8");
					Log.i(C.TAG, "首页请求结果----->" + str);
					Gson gson = new Gson();
					HomePageList homePageList = gson.fromJson(str, HomePageList.class);
					dataList = homePageList.data;
				}
				catch (Exception e)
				{
					e.printStackTrace();
				}
				Message message = new Message();
				message.what = 0;
				message.arg1 = result;
				handler.sendMessage(message);
			}
		}
	};
	
	/**
	 * 处理结果
	 */
	private Handler handler = new Handler()
	{
		
		@Override
		public void handleMessage(Message msg)
		{
			super.handleMessage(msg);
			C.MAIN_DATA = dataList;
			handler.post(runnable);
		}
		
	};
}
